package com.example.park2212.points

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.mvvm.BaseActivity
import com.example.park2212.R
import com.example.park2212.databinding.ActivityTasksBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/points/TasksActivity")
class TasksActivity : BaseActivity<ActivityTasksBinding,PointsViewModel>() {
    val pointsAdapter by lazy { PointsAdapter() }
    override fun initView() {
        viewModel.points()
        binding.tasksHead.headerTitle.setText("点位管理")
        binding.tasksHead.headerBack.setOnClickListener {
            finish()
        }


        pointsAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build( "/app/points/DetailPointsActivity")
                .withString("pid",pointsAdapter.data.get(position).id.toString()).navigation()
        }

        binding.rvTasks.apply {
            adapter = pointsAdapter
            layoutManager = LinearLayoutManager(this@TasksActivity)
        }

        binding.tasksHead.headerAdd.setOnClickListener {
            ARouter.getInstance().build("/app/points/PointsAddActivity").navigation()
        }

       pointsAdapter.setOnItemLongClickListener { adapter, view, position ->
           viewModel.deletePoints(pointsAdapter.data[position].id)
           pointsAdapter.data.removeAt(position)
           pointsAdapter.notifyItemRemoved(position)
           return@setOnItemLongClickListener true
       }



    }

    override fun initData() {
       viewModel.pointsSuccess.observe(this){
           pointsAdapter.data.clear()
           pointsAdapter.data.addAll(it)
           pointsAdapter.notifyDataSetChanged()
       }

        viewModel.deletePointsSuccess.observe(this){
            ToastUtils.showLong("删除成功")
        }
    }

    override fun onResume() {
        super.onResume()

    }
}